  {  Petrova Tatjana
     2012 / 10 / 15
     This program contains proctdures which work with lists.}
program Lists;

{$APPTYPE CONSOLE}

uses
  SysUtils;
type
  pelement =^element;
  element = record
    value : integer;
    next : pelement;
  end;

procedure AddToBegin(var head:pelement;num:integer);
var
  tmp : pelement;
begin
  tmp := head;
  new(head);
  head^.value := num;
  head^.next := tmp;
end;

procedure AddToEnd (var head:pelement;
                        num : integer);
var
  current : pelement;
begin
  if(head<>nil) then
  begin
    current:=head;
    while(current^.next<>nil)do
      current := current^.next;
    new(current^.next);
    current^.next^.value:=num;
    current^.next^.next:=nil
  end else
    AddToBegin(head,num);
  end;

procedure printList (head : pelement);
var
  current : pelement;
begin
  current := head;
  while(current<>nil) do
  begin
    writeln(current^.value);
    current := current^.next;
  end;
end;
procedure DisposeFromBeginning(var head : pelement);
var
  temp : pelement;
begin
  if(head<>nil) then
  begin
    temp:=head^.next;
    dispose(head);
    head:=temp;
  end;
end;
procedure DisposeFromEnd(var head : pelement);
var
  current : pelement;
begin
  if(head<>nil) then
  begin
    if (head^.next<>nil) then
    begin
      current := head;
      while(current^.next^.next<> nil) do
        current := current^.next;
      dispose(current^.next);
      current^.next:=nil;
    end;
  end;
end;
var
  head:pelement;
begin
  AddToBegin(head,3);
  AddToEnd(head,3);
  AddToBegin(head,8);
  AddToBegin(head,1);
  DisposeFromEnd(head);
  DisposeFromBeginning(head);
  printList(head);
  readln;
end.
